package aaGame;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

import azServiceLayer.Input;

public class RpgGamePanel extends JPanel implements Runnable {
	
	private static final long serialVersionUID = 8845254671964396040L;

	/* The default width and height of the JPanel */
	public static final int PANEL_WIDTH;
	public static final int PANEL_HEIGHT;
	GameEngine engine;
	Input keyboardInput;

	public int volume = 127;
	public Thread thread;
	
	static {
		PANEL_WIDTH = RpgGameFrame.DEFAULT_WIDTH - 16;
		PANEL_HEIGHT = RpgGameFrame.DEFAULT_HEIGHT - 38;
	}

	/* Constructor that accepts GameFrame as an argument. */
	public RpgGamePanel(RpgGameFrame frame) {
		this.setBackground(Color.black);
		engine = new GameEngine();
		keyboardInput = new Input(engine, frame);

		setFocusable(true);
		requestFocus();
		
		/* Initializes a keyListener for the InputManager class. */
		addKeyListener(keyboardInput);
		
		/* Starts the thread. */
		thread = new Thread(this);
		thread.start();
	}

	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		engine.render((Graphics2D)g, this);
	}
	
	@Override
	public void run() {
		while (true) {
			try {
				engine.update();	//updates state information
				repaint(); 			//renders the world
					Thread.sleep(8);
			} catch (InterruptedException e) {
				System.out.println("Exception: " + e.getMessage());
			}
		}
	}

}
